<style lang="less" scoped>
@import "@/styles/table-common.less";
.btn-box {
  // padding: 10px 12px 10px 0;
  height: 32px;
  display: flex;
  align-items: center;
  span {
    margin-left: 15px;
  }
}
.content {
  display: flex;
  flex-wrap: wrap;
  margin-bottom: 40px;
  /deep/.c-card {
    width: 350px;
    margin-right: 25px;
    margin-top: 15px;
    .num {
      font-size: 40px;
      margin-bottom: 20px;
    }
    .card-footer {
      display: flex;
      justify-content: space-between;
      border-top: 1px solid #e8eaec;
      padding-top: 10px;
    }
  }
}
</style> 
<template>
  <div class="search">
    <Card>
      <Form ref="searchForm" :model="searchForm" inline :label-width="100">
        <Row v-show="openSearch" @keydown.enter.native="handleSearch">
          <FormItem label="商品名称">
            <Input
              v-model="searchForm.productName"
              placeholder="请输入商品名称"
            ></Input>
          </FormItem>
          <FormItem label="所属商家">
            <Select
              v-model="searchForm.companyId"
              transfer
              placeholder="请选择"
              style="width: 200px"
            >
              <Option
                v-for="item in companyList"
                :key="item.id"
                :value="item.id"
                >{{ item.companyName }}</Option
              >
            </Select>
          </FormItem>
          <!-- <FormItem label="企业性质：">
            <Select
              v-model="searchForm.companyType"
              transfer
              placeholder="请选择"
              style="width: 200px"
            >
              <Option value="">全部</Option>
              <Option value="2">餐饮</Option>
              <Option value="3">培训</Option>
            </Select>
          </FormItem> -->
          <FormItem style="margin-left: -35px" class="br">
            <Button @click="handleSearch" type="primary" icon="ios-search"
              >搜索</Button
            >
            <Button @click="handleReset">重置</Button>
          </FormItem>
        </Row>
      </Form>

      <Table
        :loading="loading"
        border
        :columns="columns"
        :data="data"
        sortable="custom"
        ref="table"
      >
        <!-- <template slot-scope="{ row }" slot="print">{{ row.print.serialNo }}</template> -->
      </Table>
      <Row type="flex" justify="end" class="page">
        <Page
          :current="searchForm.pageNumber"
          :total="total"
          :page-size="searchForm.pageSize"
          @on-change="changePage"
          @on-page-size-change="changePageSize"
          :page-size-opts="[10, 20, 50]"
          size="small"
          show-total
          show-elevator
          show-sizer
        ></Page>
      </Row>
    </Card>

    <Modal v-model="modal" title="详情" @on-ok="ok" @on-cancel="cancel">
      <div class="detail-modal">
        <Form
          ref="form"
          :model="formSet"
          label-position="top"
          :rules="formSetRule"
        >
          <Row :gutter="32">
            <Col span="24">
              <FormItem
                :label="detailData.isHomePage==1?'确定要取消推荐吗？':'确定要推荐至首页吗？'"
                prop="num"
              >
                <!-- <Input v-model="formSet.num" placeholder="请填写数字" /> -->
              </FormItem>
            </Col>
          </Row>
        </Form>
      </div>
      <div slot="footer">
        <Button
          type="primary"
          size="large"
          :loading="submitLoading"
          @click="submit"
          >保存</Button
        >
        <Button size="large" @click="cancel">取消</Button>
      </div>
    </Modal>
  </div>
</template>

<script>
import {
  getHomePageProductByCondition,
  updateHomePageStatus,

  getScCompanygetAllMerchant,
} from "@/api/index";

import { shortcuts } from "@/libs/shortcuts";
export default {
  name: "user-manage",
  components: {},
  data() {
    return {
    
      message: "",
      showUser: false,
      showType: "0",
      loading: true,
      selectDate: "",
      openSearch: true,
      idenList: [], //角色状态
      options: {
        shortcuts: shortcuts,
      },
      orderTypeList: [
        { id: "0", title: "已取消" },
        { id: "1", title: "已完成" },
      ],
      formSetRule: {
        num: [{ required: true, message: "请输入纳入金额", trigger: "blur" }],
      },
      searchForm: {
        pageNumber: 1,
        pageSize: "10",
        sort: "createTime",
        order: "desc",
      },
      columns: [
        {
          title: "商品名称",
          key: "storeName",
          align: "center",
        },

        {
          title: "所属商家",
          key: "companyName",

          align: "center",
        },

        {
          title: "操作",
          key: "createTime",
      
          align: "center",
          fixed: "right",
          render: (h, params) => {
            let enableOrDisable = "";
            enableOrDisable = h(
              "a",
              {
                on: {
                  click: () => {
                    this.modal = true;
                    this.detailData = params.row;
                  },
                },
              },
              params.row.isHomePage  == 1 ? "取消推荐" : "推荐至首页"
            );
            return h("div", [enableOrDisable]);
          },
        },
      ],
      modal: false,
      formSet: {},
      data: [],
      detailData: [],
      companyList: [],
    };
  },
  methods: {
    init() {
      this.getScCompanygetAllMerchantData();
      this.getDataList();
    },
    changeSort(e) {
      this.searchForm.sort = e.key;
      this.searchForm.order = e.order;
      if (e.order == "normal") {
        this.searchForm.order = "";
      }
      this.getDataList();
    },
    getDataList() {
      // 多条件搜索用户列表
      this.loading = true;
      getHomePageProductByCondition(this.searchForm).then((res) => {
        this.loading = false;
        if (res.success) {
          this.data = res.result.records;
          this.total = res.result.total;
          if (this.data.length == 0 && this.searchForm.pageNumber > 1) {
            this.searchForm.pageNumber -= 1;
            this.getDataList();
          }
        }
      });
    },
    getScCompanygetAllMerchantData() {
      getScCompanygetAllMerchant().then((res) => {
        if (res.success) {
          this.companyList = res.result;
        }
      });
    },

    add() {
      this.showType = "2";
      this.showUser = true;
    },
    changePage(v) {
      this.searchForm.pageNumber = v;
      this.getDataList();
    },
    changePageSize(v) {
      this.searchForm.pageSize = v;
      this.getDataList();
    },
    selectDateRange(v) {
      if (v) {
        this.searchForm.startDate = v[0];
        this.searchForm.endDate = v[1];
      }
    },
    handleSearch() {
      this.searchForm.pageNumber = 1;
      this.searchForm.pageSize = 10;
      // return
      this.getDataList();
    },
    handleReset() {
      this.selectDate = "";
      // this.searchForm.startDate = "";
      // this.searchForm.endDate = "";
      // this.searchForm.status = "";
      // this.searchForm.companyId = "";
      // this.searchForm.pageNumber = 1;
      // this.searchForm.pageSize = 10;
      this.searchForm = {
        pageNumber: 1,
        pageSize: 10,
      };
      // 重新加载数据
      this.getDataList();
    },
    enable(v) {
      this.$Modal.confirm({
        title: "确认启用",
        content: "您确认要启用用户 " + v.title + " ?",
        loading: true,
        onOk: () => {
          enableUser(v.id).then((res) => {
            this.$Modal.remove();
            if (res.success) {
              this.$Message.success("操作成功");
              this.getDataList();
            }
          });
        },
      });
    },
    submit() {
       updateHomePageStatus({
            status:this.detailData.isHomePage ==1?0:1,
            id: this.detailData.id,
          }).then((res) => {
            if (res.success) {
              this.formSet.num = "";
              this.$Message.success("操作成功");
              this.modal = false;
              this.getDataList();
            }
          });
    },
    cancel() {
      this.modal = false;
      this.formSet.num = "";
    },
  },
  created() {},
  mounted() {
    // 计算高度

    this.init();
  },
};
</script>